<template>
  <div class="app-container">
    <el-form :inline="true" :model="searchForm" class="demo-form-inline">
      <el-form-item label="关键字">
        <el-input v-model="searchForm.keyword" placeholder="关键字"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click.native="reloadData()">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-edit" @click="dialogFormVisible = true">新建</el-button>
      </el-form-item>
    </el-form>
    <el-dialog title="新建用户" :visible.sync="dialogFormVisible" >
      <el-form :model="form" :inline="true" class="demo-form-inline">
        <el-form-item label="用户名称" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="手机号" :label-width="formLabelWidth">
          <el-input v-model="form.loginName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth">
          <el-input v-model="form.passWord" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="用户角色" :label-width="formLabelWidth">
          <el-select v-model="form.roleId" placeholder="请选择用户角色">
            <el-option
              v-for="item in options"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click.native="saveUser()">确 定</el-button>
      </div>
    </el-dialog>
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID" width="95">
        <template slot-scope="scope">
          {{ scope.row.id }}
        </template>
      </el-table-column>
      <el-table-column label="启用标识">
        <template slot-scope="scope">
          {{ scope.row.enable }}
        </template>
      </el-table-column>
      <el-table-column label="登录账号" width="110" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.loginName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="用户名称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="角色id" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.roleId }}
        </template>
      </el-table-column>
      <el-table-column label="角色名称" width="110" align="center">
        <template slot-scope="scope">
          {{ scope.row.roleName }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button @click.native="handleClick(scope.row)" type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { userList, roleList, saveUser } from '@/api/user'
import { Message } from 'element-ui'

export default {
  // 加载下拉框接口数据

  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        keyword: null
      },
      // 对话框数据渲染
      dialogTableVisible: false,
      dialogFormVisible: false,
      form: {
        userId: null,
        name: null,
        loginName: null,
        passWord: null,
        roleId: null,
        key: null,
        value: null
      },
      options: [],
      formLabelWidth: '120px'
    }
  },
  created() {
    this.fetchData()
    this.getRole()
  },
  methods: {
    fetchData(keyword) {
      this.listLoading = true
      userList({ pageSize: 10, pageCurrent: 1, keyword: keyword }).then(response => {
        console.log(response)
        this.list = response.data.records
        this.listLoading = false
      })
    },
    reloadData() {
      this.fetchData(this.searchForm.keyword)
    },
    getRole() {
      roleList({ pageSize: 10, pageCurrent: 1 }).then(response => {
        console.log(response)
        this.options = response.data
      })
    },
    saveUser() {
      saveUser(this.form).then(response => {
        console.log(response)
        this.reloadData()
        this.dialogFormVisible = false
        Message({
          message: response.msg,
          type: 'success',
          duration: 5 * 1000
        })
      })
    },
    handleClick(row) {
      console.log(row)
      this.dialogFormVisible = true
      this.form.name = row.name
      this.form.loginName = row.loginName
      this.form.roleId = row.roleId
      this.form.passWord = row.passWord
      this.form.userId = row.id
    }
  }
}
</script>
